from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
import time





def search():
    try:
        browser.get('https://www.taobao.com/')
        input = wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#q"))
        )
        submit = wait.until((EC.element_to_be_clickable((By.CSS_SELECTOR, "#J_TSearchForm > div.search-button"))))
        loginLink = browser.find_element_by_link_text("亲，请登录")
        loginLink.click()
        login = browser.find_element_by_css_selector("#J_QRCodeLogin > div.login-links > a.forget-pwd.J_Quick2Static")
        login.click()

        # browser.get("https://login.taobao.com/member/login.jhtml?spm=a21bo.2018.754894437.1.5af911d9Xtx167&f=top&redirectURL=https%3A%2F%2Fwww.taobao.com%2F")
        name = browser.find_element_by_css_selector("#TPL_username_1")
        name.send_keys("18203689906")
        password = browser.find_element_by_css_selector("#TPL_password_1")
        password.send_keys("wenyu961125.")


        try:
            element = browser.find_element_by_id('nc_1__scale_text')
            ActionChains(browser).drag_and_drop_by_offset(element, 400, 0).perform()
            # time.sleep(20)
            # browser.execute_script(js)
        except:
            print('无滑块')
            pass

        # input.send_keys('香水')
        # submit.click()
        # total = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.total')))
        # return total.text
    except TimeoutError:
        return search()



def main():
    total = search()
    print(total)


if __name__ == '__main__':
    main()